/*
 * $Id: PRCCategoryManager.java,v 1.1 2009/04/07 19:25:48 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.test;

import org.springframework.beans.factory.InitializingBean;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.IPrcToTaskCategoryDAO;
import com.ge.healthcare.autosc.common.database.dao.ITaskCategoryInfoDAO;
import com.ge.healthcare.autosc.common.database.pojo.CfgPrcToTaskCategory;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskCategoryInfo;
import com.ge.healthcare.autosc.common.dbLogger.StepLogger;

/**
 * @author 212042764
 *
 */
public class PRCCategoryManager implements InitializingBean{

	protected IPrcToTaskCategoryDAO prcToTaskCategoryDAO = null;
	protected ITaskCategoryInfoDAO taskCategoryDAO = null;
	public PRCCategoryManager(){
		super();
	}
	
	protected CfgPrcToTaskCategory buildPrcToTaskCategoryMap(CfgTaskCategoryInfo taskCategory){
		CfgPrcToTaskCategory prcToTaskCategory = prcToTaskCategoryDAO.getPrcTaskCategoryInfoForPrcServer(StepLogger.getManagedServerName(), taskCategory.getTaskCategory());
		if (ASCUtil.isNullOrEmptyObject(prcToTaskCategory)){
			prcToTaskCategory =  new CfgPrcToTaskCategory();
			prcToTaskCategory.setServerName(StepLogger.getManagedServerName());
			prcToTaskCategory.setCfgTaskCategoryInfo(taskCategory);
			prcToTaskCategory.setConcurrentConsumers(15);
			prcToTaskCategory.setMaxConcurrentConsumers(25);
			prcToTaskCategoryDAO.save(prcToTaskCategory);
		}		
		return prcToTaskCategory;
	}
	
	protected CfgTaskCategoryInfo buildTaskCategory(String taskCategory, String queueName){
		CfgTaskCategoryInfo taskCategoryInfo = taskCategoryDAO.getTaskCategoryByName(taskCategory);
		if (ASCUtil.isNullOrEmptyObject(taskCategoryInfo)){
			taskCategoryInfo = new CfgTaskCategoryInfo();
			taskCategoryInfo.setTaskCategory(taskCategory);
			taskCategoryInfo.setQueueName(queueName);
			taskCategoryDAO.save(taskCategoryInfo);
		}
		return taskCategoryInfo;
	}
	
	/**
	 * @param prcToTaskCategoryDAO the prcToTaskCategoryDAO to set
	 */
	public void setPrcToTaskCategoryDAO(IPrcToTaskCategoryDAO prcToTaskCategoryDAO) {
		this.prcToTaskCategoryDAO = prcToTaskCategoryDAO;
	}

	/**
	 * @param taskCategoryDAO the taskCategoryDAO to set
	 */
	public void setTaskCategoryDAO(ITaskCategoryInfoDAO taskCategoryDAO) {
		this.taskCategoryDAO = taskCategoryDAO;
	}

	public void afterPropertiesSet() throws Exception {
		CfgTaskCategoryInfo taskCategory = this.buildTaskCategory("sweeptst", "autosc.test.queue.sweep");
		CfgTaskCategoryInfo taskCategory2 = this.buildTaskCategory("notifytst", "autosc.test.queue.notify");
		this.buildPrcToTaskCategoryMap(taskCategory);
		this.buildPrcToTaskCategoryMap(taskCategory2);
	}
}
